import java.io.*;
class DataSet {
	Pattern[] patterns; 
	Pattern[] trainingPatterns;
	Pattern[] crossValPatterns; 
	Pattern[] testPatterns; 
	double[] crossvaldeviations;
	double[] testdeviations;

	public DataSet (String sourceFile, int numInputs, int numTargets, double trainingRatio, double crossValRatio) {
		DataReader reader = new DataReader(sourceFile);
		int counter = 0;
		double temp_double;
		while (reader.nextToken()){
			try {
				for ( int i = 0; i < (numInputs + numTargets); i++) {
					temp_double = Double.parseDouble(reader.column[i]);
				}
				counter++;
			}
			catch (NumberFormatException e) {}
		}
		reader = null;
		patterns = new Pattern[counter];

		double[] temp_in = new double[numInputs];
		double[] temp_tar = new double[numTargets];
		reader = new DataReader(sourceFile);
		counter = 0;
		while (reader.nextToken()){
			try { 
				for (int i = 0; i < numInputs; i++) {
					temp_in[i] = Double.parseDouble(reader.column[i]);
				}
				for (int i = numInputs; i < numInputs+numTargets; i++) {
					temp_tar[i-numInputs] = Double.parseDouble(reader.column[i]);
				}
				patterns[counter++] = new Pattern(temp_in, temp_tar);
			}
			catch (NumberFormatException e) {}
		}
		reader = null;
		
		trainingPatterns = new Pattern[(int)(Math.round(patterns.length * trainingRatio))];
		crossValPatterns = new Pattern[(int)(Math.round(patterns.length * crossValRatio))];
		testPatterns = new Pattern[patterns.length - trainingPatterns.length - crossValPatterns.length];
		
		int patternSelected;
		int patternsNotSelected = patterns.length;
		
		for ( int i = 0; i < trainingPatterns.length; i++ ) {
			patternSelected = RandGen.nextInt(patternsNotSelected);
			counter = 0;
			for ( int j = 0; j < patterns.length; j++ ) {
				if ( !patterns[j].selected ) {
					if ( counter == patternSelected ) {
						trainingPatterns[i] = patterns[j];
						patterns[j].selected = true;
						patternsNotSelected--;
						break;
					}
					counter++;
				}
			}
		}
		
		for ( int i = 0; i < crossValPatterns.length; i++ ) {
			patternSelected = RandGen.nextInt(patternsNotSelected);
			counter = 0;
			for ( int j = 0; j < patterns.length; j++ ) {
				if ( !patterns[j].selected ) {
					if ( counter == patternSelected ) {
						crossValPatterns[i] = patterns[j];
						patterns[j].selected = true;
						patternsNotSelected--;
						break;
					}
					counter++;
				}
			}
		}
		
		for ( int i = 0; i < testPatterns.length; i++ ) {
			patternSelected = RandGen.nextInt(patternsNotSelected);
			counter = 0;
			for ( int j = 0; j < patterns.length; j++ ) {
				if ( !patterns[j].selected ) {
					if ( counter == patternSelected ) {
						testPatterns[i] = patterns[j];
						patterns[j].selected = true;
						patternsNotSelected--;
						break;
					}
					counter++;
				}
			}
		}
		
		for ( int i = 0; i < patterns.length; i++ ) patterns[i].selected = false;

		double[] avg = new double[numTargets];
		crossvaldeviations = new double[numTargets];
		testdeviations = new double[numTargets];
		for (int i = 0; i < numTargets; i++) {
			avg[i] = 0;
			for (int j = 0; j < crossValPatterns.length; j++) avg[i] += crossValPatterns[j].target[i];
			avg[i] /= crossValPatterns.length;
			crossvaldeviations[i] = 0;
			for (int j = 0; j < crossValPatterns.length; j++)
				crossvaldeviations[i] += Math.abs(crossValPatterns[j].target[i] - avg[i]);
			crossvaldeviations[i] = crossvaldeviations[i] * 2 / crossValPatterns.length;
			
			avg[i] = 0;
			for (int j = 0; j < testPatterns.length; j++) {
				avg[i] += testPatterns[j].target[i];
			}
			avg[i] /= testPatterns.length;
			testdeviations[i] = 0;
			for (int j = 0; j < testPatterns.length; j++) {
				testdeviations[i] += Math.abs(testPatterns[j].target[i] - avg[i]);
			}
			testdeviations[i] = testdeviations[i] * 2 / testPatterns.length;
		}	
	}
	public class DataReader{
		FileInputStream fis;
		String[] column;
		BufferedInputStream bis;
		public DataReader (String path) {
			try {
				fis = new FileInputStream(path);
				bis = new BufferedInputStream(fis);
			}	      
			catch (IOException e) {}
		}
		public boolean nextToken () {
			column = null;
			column = nextLine().split(";");
			if (column[0] != "#EOF#") {
				for (int i = 0; i < column.length; i++) {
					column[i] = column[i].trim();
				}
				return true;
			}
			else{return false;}
		}
		public String nextLine() {
			int i;
			char[] temp_array = new char[50000];
			char[] temp_array2;
			boolean last_line;
			int counter;
			String temp_line = "";
		
			do {
				temp_array2 = null;
				counter = 0;
				last_line = true;
				try {
					while ( (i = bis.read()) != -1 ) {
						last_line = false;
						if (i == 13 || i == 10) {
							break;
						}
						else if( i != 10 && i != 13) {
							temp_array[counter++] = (char)i;
						}
					}
				}
				catch (IOException e) {}
				if (last_line) {
					temp_line = "#EOF#";
				}
				else if (counter != 0) {
					temp_array2 = new char[counter];
					boolean all_spaces = true;
					for (int j = 0; j < counter; j++) {
						if (temp_array[j] != ' ') {all_spaces = false;}
						temp_array2[j] = temp_array[j];
					}
					if (all_spaces) {temp_line = "";}
					else {temp_line = new String(temp_array2);}
					if (temp_line.length() >= 2 && temp_line.charAt(0) == '/' && temp_line.charAt(1) == '/') {
						temp_line = "";
					}
				}
				else {
					temp_line = "";
				}
			} while (temp_line == "");
			return temp_line.trim();
		}
	}	
}